/*
 * Copyright 2012-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.stars.easyms.logger.support.logback.springboot.version1xx;

import ch.qos.logback.core.joran.action.Action;
import ch.qos.logback.core.joran.action.ActionUtil;
import ch.qos.logback.core.joran.spi.ActionException;
import ch.qos.logback.core.joran.spi.InterpretationContext;
import ch.qos.logback.core.util.OptionHelper;
import com.stars.easyms.logger.support.logback.springboot.version1xx.bind.EasyMsRelaxedPropertyResolver;
import org.springframework.core.env.Environment;
import org.xml.sax.Attributes;

/**
 * <p>className: EasyMsSpringPropertyAction</p>
 * <p>description: 因原SpringPropertyAction权限问题新增相同代码的类</p>
 *
 * @author guoguifang
 * @date 2019-11-14 16:35
 * @since 1.4.0
 */
class EasyMsSpringPropertyAction extends Action {

    private static final String SOURCE_ATTRIBUTE = "source";

    private static final String DEFAULT_VALUE_ATTRIBUTE = "defaultValue";

    private final Environment environment;

    EasyMsSpringPropertyAction(Environment environment) {
        this.environment = environment;
    }

    @Override
    public void begin(InterpretationContext ic, String elementName, Attributes attributes)
            throws ActionException {
        String name = attributes.getValue(NAME_ATTRIBUTE);
        String source = attributes.getValue(SOURCE_ATTRIBUTE);
        ActionUtil.Scope scope = ActionUtil.stringToScope(attributes.getValue(SCOPE_ATTRIBUTE));
        String defaultValue = attributes.getValue(DEFAULT_VALUE_ATTRIBUTE);
        if (OptionHelper.isEmpty(name) || OptionHelper.isEmpty(source)) {
            addError(
                    "The \"name\" and \"source\" attributes of <springProperty> must be set");
        }
        ActionUtil.setProperty(ic, name, getValue(source, defaultValue), scope);
    }

    private String getValue(String source, String defaultValue) {
        if (this.environment == null) {
            addWarn("No Spring Environment available to resolve " + source);
            return defaultValue;
        }
        String value = this.environment.getProperty(source);
        if (value != null) {
            return value;
        }
        int lastDot = source.lastIndexOf('.');
        if (lastDot > 0) {
            String prefix = source.substring(0, lastDot + 1);
            EasyMsRelaxedPropertyResolver resolver = new EasyMsRelaxedPropertyResolver(
                    this.environment, prefix);
            return resolver.getProperty(source.substring(lastDot + 1), defaultValue);
        }
        return defaultValue;
    }

    @Override
    public void end(InterpretationContext ic, String name) throws ActionException {
        // Intentionally blank
    }
}